package com.zjq.springsocial.renren.android.ui;

import org.springframework.social.connect.ConnectionRepository;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;

import com.zjq.springsocial.renren.android.R;
import com.zjq.springsocial.renren.android.RenrenAppliction;
import com.zjq.springsocial.renren.android.ui.auth.RenrenAuthzActivity;
import com.zjq.springsocial.renren.api.Renren;
import com.zjq.springsocial.renren.connect.RenrenConnectionFactory;

/**
 * 
 * <P>
 * 人人网主页
 * </P>
 * 
 * @author zhongjingquan
 * 
 * @version 0.0.1-SNAPSHOT 2013-7-19上午11:48:49
 */
public class RenrenIndexActivity extends Activity {

	protected static final String TAG = RenrenIndexActivity.class.getSimpleName();
	//连接资源对象
	private ConnectionRepository connectionRepository;
	//连接工厂
	private RenrenConnectionFactory connectionFactory;
	//应用上下文对象
	private RenrenAppliction renrenAppliction;

	@Override
	protected void onCreate(Bundle inState) {
		super.onCreate(inState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.authz_activity);
		// 获取图片按钮
		ImageButton imageButton = (ImageButton) findViewById(R.id.authz_btn);
		imageButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(RenrenIndexActivity.this, RenrenAuthzActivity.class);
				startActivity(intent);
			}
		});
		renrenAppliction = (RenrenAppliction) getApplicationContext();
		this.connectionRepository = renrenAppliction.getConnectionRepository();
		this.connectionFactory = renrenAppliction.getRenrenConnectionFactory();
	}
	
	private void forwordRenrenMainActivity() {
		Intent intent = new Intent();
		intent.setClass(this, RenrenMainActivity.class);
		startActivity(intent);
		finish();
	}
	
	@Override
	public void onStart() {
		super.onStart();
		if (isConnected()) {
			forwordRenrenMainActivity();
		}
	}
	
	
	/**
	 * 是否连接成功
	 * @return
	 */
	private boolean isConnected() {
		return connectionRepository.findPrimaryConnection(Renren.class) != null;
	}

	/**
	 * 断开连接
	 */
	private void disconnect() {
		this.connectionRepository.removeConnections(this.connectionFactory.getProviderId());
	}
}
